﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entity = VietStar.Library.Entity;
using Business = VietStar.Library.Business;
using VietStar.Library.Commom.Module;

public partial class controls_client_listproducts : System.Web.UI.UserControl
{
    Entity.SanPham clsE_SanPham = new VietStar.Library.Entity.SanPham();
    Business.SanPham clsB_SanPham = new VietStar.Library.Business.SanPham();
    Entity.DanhMucSanPham clsE_DanhMucSanPham = new VietStar.Library.Entity.DanhMucSanPham();
    Business.DanhMucSanPham clsB_DanhMucSanPham = new VietStar.Library.Business.DanhMucSanPham();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string action = Request.QueryString["action"].ToString();
            string danhmuc = Request.QueryString["danhmuc"].ToString();
            LoadSanPhamByAction(action, danhmuc);
        }
    }
    #region Phân trang bằng PagedDataSource
    private static DataTable dtSanPham = new DataTable();
    //Khái báo các property trang, chỉ số đầu, chỉ số cuối
    private int CurrentPage
    {
        get
        {
            object objPage = ViewState["_CurrentPage"];

            int _CurrentPage = 0;
            if (objPage == null)
            {
                _CurrentPage = 0;
            }
            else
            {
                _CurrentPage = (int)objPage;
            }
            return _CurrentPage;
        }
        set { ViewState["_CurrentPage"] = value; }
    }
    private int fistIndex
    {
        get
        {

            int _FirstIndex = 0;
            if (ViewState["_FirstIndex"] == null)
            {
                _FirstIndex = 0;
            }
            else
            {
                _FirstIndex = Convert.ToInt32(ViewState["_FirstIndex"]);
            }
            return _FirstIndex;
        }
        set { ViewState["_FirstIndex"] = value; }
    }
    private int lastIndex
    {
        get
        {
            int _LastIndex = 0;
            if (ViewState["_LastIndex"] == null)
            {
                _LastIndex = 0;
            }
            else
            {
                _LastIndex = Convert.ToInt32(ViewState["_LastIndex"]);
            }
            return _LastIndex;
        }
        set { ViewState["_LastIndex"] = value; }
    }

    PagedDataSource _PageDataSource = new PagedDataSource();

    //Load dữ liệu và phân trang cho dữ liệu
    private void BindItemsList(DataTable dt)
    {
        DataTable dataTable = dt;
        _PageDataSource.DataSource = dataTable.DefaultView;
        _PageDataSource.AllowPaging = true;
        _PageDataSource.PageSize = 10;
        _PageDataSource.CurrentPageIndex = CurrentPage;
        ViewState["TotalPages"] = _PageDataSource.PageCount;

        this.lblPageInfo.Text = "Trang " + (CurrentPage + 1) + " của " + _PageDataSource.PageCount;
        this.lbtnPrevious.Enabled = !_PageDataSource.IsFirstPage;
        this.lbtnNext.Enabled = !_PageDataSource.IsLastPage;
        this.lbtnFirst.Enabled = !_PageDataSource.IsFirstPage;
        this.lbtnLast.Enabled = !_PageDataSource.IsLastPage;

        this.DataListSanPham.DataSource = _PageDataSource;
        this.DataListSanPham.DataBind();
        this.doPaging();
    }
    //Phân trang
    private void doPaging()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("PageIndex");
        dt.Columns.Add("PageText");
        fistIndex = CurrentPage - 5;
        if (CurrentPage > 5)
        {
            lastIndex = CurrentPage + 5;
        }
        else
        {
            lastIndex = 10;
        }
        if (lastIndex > Convert.ToInt32(ViewState["TotalPages"]))
        {
            lastIndex = Convert.ToInt32(ViewState["TotalPages"]);
            fistIndex = lastIndex - 10;
        }
        if (fistIndex < 0)
        {
            fistIndex = 0;
        }
        for (int i = fistIndex; i < lastIndex; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = i;
            dr[1] = i + 1;
            dt.Rows.Add(dr);
        }
        this.dlPaging.DataSource = dt;
        this.dlPaging.DataBind();
    }

    //Trang dầu tiên
    protected void lbtnFirst_Click(object sender, EventArgs e)
    {
        CurrentPage = 0;
        BindItemsList(dtSanPham);
    }
    //Trang sau trang đang hiển thị
    protected void lbtnPrevious_Click(object sender, EventArgs e)
    {
        CurrentPage -= 1;
        BindItemsList(dtSanPham);
    }
    //Trang tiếp theo trang đang hiển thị
    protected void lbtnNext_Click(object sender, EventArgs e)
    {
        CurrentPage += 1;
        BindItemsList(dtSanPham);
    }
    //Trang cuối
    protected void lbtnLast_Click(object sender, EventArgs e)
    {
        CurrentPage = (Convert.ToInt32(ViewState["TotalPages"]) - 1);
        BindItemsList(dtSanPham);
    }
    //Hàm sự kiện dlPaging
    protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.Equals("Paging"))
        {
            CurrentPage = Convert.ToInt32(e.CommandArgument.ToString());
            BindItemsList(dtSanPham);
        }
    }
    protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
        if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
        {
            lnkbtnPage.Enabled = false;
            //lnkbtnPage.Style.Add("fone-size", "14px");
            //lnkbtnPage.Font.Bold = true;
            //lnkbtnPage.CssClass = "active";
        }
    }
    #endregion
    private void LoadSanPhamByAction(string action, string danhmuc)
    {
            clsE_SanPham.action = action;
            laytenchuyenmuc(Convert.ToInt32(danhmuc));
            clsE_SanPham.IdDanhMuc = Convert.ToInt32(danhmuc);
            dtSanPham = clsB_SanPham.SelectSanPhamByAction(clsE_SanPham);     

        if (dtSanPham.Rows.Count == 0)
        {
            BindItemsList(dtSanPham);
            PhanTrang.Visible = false;
            lbReport.Visible = true;
        }
        else if (dtSanPham.Rows.Count > 0 && dtSanPham.Rows.Count <= 10)
        {
            BindItemsList(dtSanPham);
            PhanTrang.Visible = false;
            lbReport.Visible = false;
        }
        else if (dtSanPham.Rows.Count > 10)
        {
            BindItemsList(dtSanPham);
            PhanTrang.Visible = true;
            lbReport.Visible = false;
        }
    }
    private void laytenchuyenmuc(int idchuyenmuc)
    {
        clsE_DanhMucSanPham.Id = idchuyenmuc;
        DataTable dt = clsB_DanhMucSanPham.SelectPrimarykey(clsE_DanhMucSanPham);
        lbTitle.Text = dt.Rows[0]["Ten"].ToString();
        Page.Title = "Danh sách sản phẩm " +dt.Rows[0]["Ten"].ToString();
    }
    protected void DataListSanPham_ItemCommand(object source, DataListCommandEventArgs e)
    {
        int index = e.Item.ItemIndex;
        int key = Convert.ToInt32(DataListSanPham.DataKeys[index].ToString());
        clsE_SanPham.Id = key;
        if (e.CommandName.Equals("buy"))
        {
            DataTable dt = clsB_SanPham.SelectPrimarykey(clsE_SanPham);
            if (dt.Rows.Count > 0)
            {
                Cart cart = (Cart)Session["cart"];
                if (cart.AddItem(key, dt.Rows[0]["TenSanPham"].ToString(), Convert.ToDouble(dt.Rows[0]["DonGia"])))
                {
                    //update cartviewer
                    Control control = Page.Form.FindControl("yourcart");
                    Label count = (Label)FindControls.Find("count", control);
                    Label price = (Label)FindControls.Find("price", control);
                    if (count != null && count != null)
                    {
                        count.Text = cart.CartData.Count + " sản phẩm";
                        price.Text = cart.TotalPrice + " vnđ";
                    }
                }
            }
        }
    }
}
